﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Jzy.Workflow.Flow;
using Jzy.Workflow.Form;
using Jzy.Workflow.Task;
using Frame.Lib.Utilities;
using Frame.Lib.Common;


namespace Jzy.Web.WorkFlow.Fore
{
    public partial class TaskList :PageList
    {
        private FlowTaskBO _task = new FlowTaskBO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                drpSortDirection.Items.Add(new ListItem("升序", "asc"));
                drpSortDirection.Items.Add(new ListItem("降序", "desc"));
                drpSortDirection.SelectedValue = "desc";

                drpSortExpression.Items.Add(new ListItem("到达时间", "SendTime"));
                drpSortExpression.Items.Add(new ListItem("发送人", "SendID"));
                drpSortExpression.Items.Add(new ListItem("任务来源", "FromType"));
                drpSortExpression.Items.Add(new ListItem("拟稿人", "InputTime"));
                drpSortExpression.Items.Add(new ListItem("拟稿时间", "CreatorName"));
                drpSortExpression.SelectedValue = "SendTime";

                BindView();
            }
            JSUtil.SetTableHeight("main", 28);
        }

        protected string GetReadImage(object readTime)
        {
            if (readTime == null)
            {
                return "~/common/images/workflow/mailunread.gif";
            }
            else
            {
                return "~/common/images/workflow/mailread.gif";
            }
        }

        protected string GetReadStyle(object readTime)
        {
            if (readTime == null)
            {
                return "font-weight:bold;";
            }
            else
            {
                return "";
            }
        }

        private void BindView()
        {
            List<FlowTaskInfo> tasks = _task.GetRequiredList(AppSession.UserID);
            IEnumerable<FlowTaskInfo> list = null;
            if (drpSortDirection.SelectedValue == "asc")
            {
                list = tasks.OrderBy(new Func<FlowTaskInfo, object>(Sort));
            }
            else
            {
                list = tasks.OrderByDescending(new Func<FlowTaskInfo, object>(Sort));
            }

            lv.DataSource = list;
            lv.DataBind();
        }

        private object Sort(FlowTaskInfo info)
        {
            string field = drpSortExpression.SelectedValue;
            switch (field)
            {
                case "InputTime":
                case "CreatorName":
                    return info.FormInfo.GetType().GetProperty(field).GetValue(info.FormInfo, null);
                default:
                    return info.GetType().GetProperty(field).GetValue(info,null);
            }
        }

        protected string GetFromType(object fromType)
        {
            FromType type = (FromType)(int)fromType;
            if (type == FromType.退回)
            {
                return "<span style='font-weight:bold;color:red;'>" + type + "</span>";
            }
            else
            {
                return type.ToString();
            }
        }
       
        protected FlowFormInfo GetForm(object form)
        {
            return (FlowFormInfo)form;
        }

        protected void drpSort_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindView();
        }
    }
}
